﻿using System;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;

namespace RibbonEditor.Forms.Publishers
{
    public partial class CreatePublisherDialog : Form
    {
        IOrganizationService innerService;

        public CreatePublisherDialog(IOrganizationService service, Entity publisher)
        {
            InitializeComponent();
            this.innerService = service;

            if (publisher != null)
            {
                this.strName.Value = publisher["uniquename"].ToString();
                this.strDisplayName.Value = publisher["friendlyname"].ToString();
                this.strPrefix.Value = publisher["customizationprefix"].ToString();
                this.icOptionValuePrefix.Value = (int)publisher["customizationoptionvalueprefix"];
                this.textBox1.Text = publisher.Contains("description") ? publisher["description"].ToString() : string.Empty;

                this.strName.Enabled = false;
                this.strDisplayName.Enabled = false;
                this.strPrefix.Enabled = false;
                this.icOptionValuePrefix.Enabled = false;
                this.textBox1.Enabled = false;
                this.lblDescription.Enabled = false;

                this.btnValidate.Visible = false;
                this.btnCancel.Visible = false;

                this.lblHeader.Text = "Publisher properties";
                this.Text = "Publisher properties dialog";
            }
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.strDisplayName.Value) || string.IsNullOrEmpty(this.strDisplayName.Value))
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    Entity publisher = new Entity("publisher");
                    publisher["uniquename"] = this.strName.Value;
                    publisher["friendlyname"] = this.strDisplayName.Value;
                    publisher["customizationprefix"] = this.strPrefix.Value;
                    publisher["customizationoptionvalueprefix"] = this.icOptionValuePrefix.Value;
                    publisher["description"] = this.textBox1.Text;

                    Guid publisherId = this.innerService.Create(publisher);
                    publisher["id"] = publisherId;

                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "Error while creating publisher: " + error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
